<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of LogDAO
 *
 * @author thales
 */
class LogDAO {
    private $conn = null;
    private static $instance = null;


    private function __construct() {
        $this->conn = DB::getConn();
    }
    
    public static function getInstance(){
        if (is_null ( self::$instance )) {
            self::$instance = new self ();
        }
        return self::$instance;
}


    public function salvar($metodo, $idObj, $obj){
        $idUsuario = SessionController::getInstance()->getAttribute('funcionario')->getUsuario()->getId();
        $request = new RequestController();
        $param = $request->getAllParams();
        $parametros = '';
        foreach ($param as $key => $value) {
            $parametros .= $key . '=>' . $value . '<br>';
        }
        
        $sql = 'insert into log
                    (
                        dtCriacao,
                        acao,
                        idItem,
                        itemDesc,
                        Usuario_id,
                        parametros
                    )
                    values
                    (
                        NOW(),
                        ?,
                        ?,
                        ?,
                        ?,
                        ?
                    )';
        
        $stt = $this->conn->prepare($sql);  
        $stt->bindValue(1, $metodo, PDO::PARAM_STR);
        $stt->bindValue(2, $idObj, PDO::PARAM_STR);
        $stt->bindValue(3, $obj, PDO::PARAM_STR);
        $stt->bindValue(4, $idUsuario, PDO::PARAM_STR);
        $stt->bindValue(5, $parametros, PDO::PARAM_STR);
        if ( !$stt->execute() ){
            throw new Exception('Não foi possível salvar o log!<br>' . var_dump($stt->errorInfo()) );
        } 
        
        return true;
    }
}

?>
